<template>
  <div class="loginBox">
    <div class="content">
      <!-- logo区域   -->
      <div class="logo">
        <img src="@/assets/img/logo.png" alt="">
      </div>

      <!-- login表单区域   -->
      <div class="loginForm">
        <el-form :model="form" label-width="auto" :rules="form.rules" ref="ruleFormRef">
          <el-form-item label="用户名" prop="name">
            <el-input v-model="form.name" />
          </el-form-item>

          <el-form-item label="密码" prop="password">
            <el-input v-model="form.password" type="password"/>
          </el-form-item>

          <el-form-item class="btn">
            <el-button type="primary" @click="onSubmit(ruleFormRef)">登录</el-button>
            <el-button>重置</el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script lang="ts" setup>
import { reactive, ref } from 'vue';
import { useRouter } from 'vue-router';
import type { FormInstance } from 'element-plus'
import { UserLogin } from '@/api/login'
import { Users } from '@/pinia/store'

  const ruleFormRef = ref<FormInstance>()
  const router = useRouter()
  const { saveToken } = Users()
  const form = reactive({
    name: '',
    password: '',
    rules: {
      name: [
        {required: true, message: '请输入用户名', trigger: 'blur'},
        {min: 3, max: 5, message: '长度在3-5个字符', trigger: 'blur'}
      ],
      password: [
        {required: true, message: '请输入密码', trigger: 'blur'},
        {min: 3, max: 7, message: '长度在3-7个字符', trigger: 'blur'}
      ]
    }
  })

  const onSubmit = async (formEl: FormInstance | undefined) => {
    const res = await UserLogin({
      username: form.name,
      password: form.password
    })

    if(!formEl) return
    formEl.validate((valid) => {
      if(valid && res.data.meta.status === 400) {
        ElMessage.error({message: res.data.meta.msg})
      } else {
        ElMessage.success({message: '登录成功', duration: 1000, onClose: () => {
          router.push('/')
          saveToken(res.data.data.token)
        }})
      }
    })
  }
</script>

<style lang="scss" scoped>
.loginBox{
  background: #2b4b6b;
  width: 100%;
  height: 100vh;
  position: relative;
  .content{
    width: 450px;
    height: 260px;
    background: #fff;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    border-radius: 4px;
    .logo{
      width: 130px;
      height: 130px;
      position: absolute;
      left: 50%;
      transform: translate(-50%, -50%);
      border: 1px solid #ccc;
      border-radius: 50%;
      padding: 10px;
      background: #fff;
      box-shadow: 0 0 10px #ccc;
      img{
        background: #ccc;
        border-radius: 50%;
        width: 100%;
        height: 100%;
      }
    }
    .loginForm{
      width: 100%;
      padding: 0 20px;
      position: absolute;
      bottom: 0;
      box-sizing: border-box;
      .btn{
        :deep .el-form-item__content{
          justify-content: end;
        }
      }
    }
  }
}
</style>